/**
 * TriggerTag.java
 * <p/>
 * Copyright (c) 2010 Teamnet International All Rights Reserved.
 * <p/>
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Prj.Manager .
 */
package ro.prjmanager.core.list;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;


public class TriggerTag extends BodyTagSupport {
    private static final Log log = LogFactoryImpl.getLog(TriggerTag.class);
    private String id;
    private String htmlSourceId;
    private String eventSource;
    private String htmlDestId;
    private String eventDest;
    PageContext pageContext;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHtmlSourceId() {
        return htmlSourceId;
    }

    public void setHtmlSourceId(String htmlSourceId) {
        this.htmlSourceId = htmlSourceId;
    }

    public String getEventSource() {
        return eventSource;
    }

    public void setEventSource(String eventSource) {
        this.eventSource = eventSource;
    }

    public String getHtmlDestId() {
        return htmlDestId;
    }

    public void setHtmlDestId(String htmlDestId) {
        this.htmlDestId = htmlDestId;
    }

    public String getEventDest() {
        return eventDest;
    }

    public void setEventDest(String eventDest) {
        this.eventDest = eventDest;
    }

    public PageContext getPageContext() {
        return pageContext;
    }

    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    @Override
    public int doAfterBody() throws JspException {
        StringBuilder sb=new StringBuilder("<script type=\"text/javascript\">\n");
        sb.append("$(\"#").append(htmlSourceId).append("\").bind('").append(eventSource).append("',function(){\n");
        sb.append("$(\"#").append(htmlDestId).append("\").trigger('").append(eventDest).append("');});\n");
        sb.append("</script>");
        try {
            getBodyContent().getEnclosingWriter().println(sb.toString());
        } catch (IOException e) {
            log.error(e);
        }
        return SKIP_BODY;
    }
}
